﻿Public Class frmContato
    Inherits frmPadrao
#Region "Declaração de variáveis"
    Private iIDContato As Int32
    Private iIDCliente As Int32
    Private iIDDepto As Int32
    Private dsContato As New DataSet
    Private sNomeCliente As String
    Private sMensagemValidacao As String = ""

    Private objContato As New Controle.ctrContato
#End Region
#Region "Propriedades"
    Public Property IDContato()
        Get
            IDContato = iIDContato
        End Get
        Set(ByVal value)
            iIDContato = value
        End Set
    End Property
    Public ReadOnly Property Contatos()
        Get
            Return dsContato
        End Get
    End Property
    Public Property IDCliente()
        Get
            IDCliente = iIDCliente
        End Get
        Set(ByVal value)
            iIDCliente = value
        End Set
    End Property
    Public WriteOnly Property NomeCliente()
        Set(ByVal value)
            sNomeCliente = value
        End Set
    End Property

#End Region
#Region "Métodos"
    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True

        If Me.txtNome.Text = String.Empty Then
            sMensagemValidacao = "- Nome do contato obrigatório."
            bRetorno = False
        End If

        Return bRetorno

    End Function
#End Region

    Private Sub frmContato_alteraModo(ByVal eModoAtual As frmPadrao.eModo) Handles Me.alteraModo

        fraCodigo.Enabled = Not (eModoAtual = eModo.eAlterando)

        Me.txtNome.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtIDepto.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtDepto.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtEmail.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtTelefone.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtCelular.Enabled = (eModoAtual = eModo.eAlterando)
        Me.txtFax.Enabled = (eModoAtual = eModo.eAlterando)
        Me.cmdProcuraDepto.Enabled = (eModoAtual = eModo.eAlterando)

    End Sub

    Private Sub frmContato_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = Me.Text & " - " & sNomeCliente
        Me.cmdNovo_Click(sender, e)
        Me.modoAtual = eModo.eAlterando

        dsContato = objContato.selecionarContato(Me.iIDContato, Me.iIDCliente, False)
        CarregarDados()
    End Sub

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click

        Dim drContato As DataRow
        Try
            If (Me.validarFormulario) Then
                If (dsContato.Tables("OTH_Contato").Select("IDContato =" & IDContato).Length > 0) Then
                    drContato = dsContato.Tables("OTH_Contato").Select("IDContato =" & IDContato)(0)
                Else
                    drContato = dsContato.Tables("OTH_Contato").NewRow()
                End If

                With drContato
                    .Item("NomeContato") = txtNome.Text.Trim
                End With
                'bRetorno = Me.objContato.salvar(Me.Codigo, _
                '                                Me.IDCliente, _
                '                                Me.txtNome.Text.Trim, _
                '                                Me.iIDDepto, _
                '                                Me.txtTelefone.Text.Trim, _
                '                                Me.txtCelular.Text.Trim, _
                '                                Me.txtFax.Text.Trim, _
                '                                Me.txtEmail.Text.Trim)



                'If bRetorno Then
                '    Me.IDContato = Me.objContato.IDContato
                '    Me.txtIdContato.Text = Me.IDContato
                '    MyBase.Codigo = Me.IDContato
                '    MyBase.modoAtual = eModo.eAguardando
                'End If
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
                Me.cmdCancelar.Text = "Cancelar"
                MyBase.modoAtual = eModo.eAlterando
                Exit Sub
            End If
            dsContato.Tables("OTH_Contato").ImportRow(drContato)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
            MyBase.modoAtual = eModo.eAlterando
        End Try

    End Sub
    Private Sub CarregarDados()
        Dim drLinha() As DataRow

        drLinha = dsContato.Tables("OTH_Contato").Select("IDContato = " & IDContato)

        Try


            If (drLinha.Count > 0) Then
                With drLinha(0)
                    txtIdContato.Text = .Item("idContato").ToString
                    txtNome.Text = .Item("NomeContato").ToString
                    txtEmail.Text = .Item("Email").ToString
                    txtTelefone.Text = .Item("Telefone").ToString
                    txtCelular.Text = .Item("Celular").ToString
                    txtFax.Text = .Item("Fax").ToString

                End With
            End If


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNovo.Click

    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub
End Class
